home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / SAT 2.1.2 / Misc / transSkel.p < prev   
Encoding:
Text File  |  1994-05-15  |  48.8 KB  |  1,472 lines  |  [TEXT/PJMM]

  1. {    TransSkel version 2.00 - Transportable application skeleton}
  2.  
  3. {    TransSkel is public domain and was originally written in LightSpeed C by:}
  4.  
  5. {            Paul DuBois}
  6. {            Wisconsin Regional Primate Research Center}
  7. {            1220 Capital Court}
  8. {            Madison WI  53706  USA}
  9.  
  10. {    UUCP:    [allegra,ihnp4,seismo]!uwvax!rhesus!dubois }
  11. {    ARPA:    dubois@rhesus.primate.wisc.edu}
  12. {    The Pascal Version of TransSkel is public domain and was ported and changed by        }
  13.  
  14. {            Owen Hartnett            }
  15. {            Ωhm Software            }
  16. {            163 Richard Drive        }
  17. {            Tiverton, RI 02878        }
  18.  
  19. {    CSNET:    omh@cs.brown.edu.CSNET                                             }
  20. {    ARPA:        omh%cs.brown.edu                                                    }
  21. {    UUCP:        [ihnp4,allegra]!brunix !omh                                            }
  22.  
  23. {    This version of TransSkel written for Lightspeed Pascal.  Lightspeed Pascal is a}
  24. {    trademark of:}
  25. {            THINK Technologies, Inc}
  26. {            420 Bedford Street  Suite 350}
  27. {            Lexington, MA  02173  USA}
  28.  
  29. {  History}
  30. {  06/13/86    Beta version. (pd) }
  31. {  08/27/86    Version number changed to 1.01.(pd)}
  32. {              v1.0 DoGrow bug fixed - the port at the point of the}
  33. {              InvalRect could have been anything; the fix is to set}
  34. {              the port to the grown window first.  This also explains}
  35. {              why the kludge to DoActivate in v1.0 worked.(pd)}
  36. {  10/02/86    Version number changed to 1.02, as a result of adding}
  37. {            modifications by David W. Berry (well!dwb@lll-lcc.arpa)}
  38. {            for supporting window zooming.  Also used his modifications}
  39. {            for supporting modeless dialogs (though not in the same}
  40. {            form).  Dialogs can be #define'd on or off.(pd)}
  41.  
  42. {12/ 28 / 86 Version number changed to 1.03 . Modified to work under LightspeedC v . 2.01 }
  43. {            - took out definitions for window zooming stuff , as it is now supported by the compiler}
  44. {             directly . Also declared DoZoom static , fixing an oversight . ( pd )}
  45. { 01 / 18 / 86 Put a SetPort into DoZoom - ZoomWindow requires port to be}
  46. {            set to window being zoomed . ( pd )}
  47. { 02 / 05 / 86 Version number changed to 1.04 . Big change : port setting behavior made explicit}
  48. {             - the only persistant switch occurs when a window comes active . This changes }
  49. {            underlying programming model ( see manual for detailed discussion ) . Thanks to}
  50. {             Duane Williams for pointing out that this should be done . Typedef 'd }
  51. {            integer/long variables to Integer, Longint to facilitate coversion to other C }
  52. {            compilers . More complete type-casting done . LightspeedC does a lot of it }
  53. {            automatically , other compilers may not . ( pd - this version never released ) }
  54. {03 / 02 / 87 Fixed bug whereby clicks in drag region of non - active windows may not bring }
  55. {            window to front . Seems to be due to DragWindow calling StillDown to see    if mouse is still }
  56. {            down .    If the machine was busy    otherwise when click occurred and }
  57. {            mouse already up when DragWindow is called , the click ends up being ignored . }
  58. {            Thanks to Roger Humphrey for finding this one . }
  59.  
  60. {* * * Changes implemented first by omh to Pascal Version}
  61.  
  62. { 12/24/86  Finished first Pascal version.   Dialogs cannot be defined off.     (omh)}
  63.  
  64. {4 / 18 / 87 Changed Desk Accessory code so it 's more tolerant of memory}
  65. {            conditions for desk accessories . ( omh ) }
  66. {7 / 12 / 87 Added "cache " code to GetWDHandler . Now TransSkel figures }
  67. {            that an event is most likely to occur for the same window as the previous }
  68. {            event . Thus the WindowPtr and WDHandle for events are cached and examined }
  69. {            to avoid searching through the handler list . ( omh ) }
  70. {7 / 12 / 87 Excised the notorious "SetPort "excess . As pointed out by Duane Williams ,}
  71. {             SetPort traps abounded unnecessarily in version 1.02 . These have been eliminated }
  72. {            now with two exceptions . First , the port is set when a window handler }
  73. {            is installed . The justification for this is that when a handler is installed , it }
  74. {            is likely that further processing will be done on it immediately . The application gets }
  75. {            control immediately after the handler is installed anyway , so this behavior can be manually }
  76. {            overridden where necessary . Second , when a window is activated , the port is}
  77. {            set to it . This follows the model of keeping the port in sync with the }
  78. {            active window . ( omh ) }
  79. {7 / 14 / 87 Added grow zone function installation    and MoreMasters to SkelInit , }
  80. {            which now requires two parameters . The first indicates the number of times to call }
  81. {            MoreMasters . The second is a ProcPtr indicating a user - supplied grow zone}
  82. {            function to be called when memory problems occur .  If nil , no grow zone}
  83. {            function is    installed . ( omh ) }
  84. {7 / 14 / 87 SkelMenu , SkelWindow , and SkelDialog now return zero or non - zero to indicate }
  85. {            failure or success of handler allocation . This could break * all * previous TransSkel }
  86. {            applications ( as will the change to SkelInit , above . Please see the section "How to }
  87. {            adapt old TransSkel to New "    in the manual    for detailed specifications on }
  88. {            how to convert your old programs . TransSkel becomes more memory conscious}
  89. {            with these changes . The functions SkelMenu , SkelWindow , and }
  90. {            SkelDialog are the only routines which actually allocate memory . Since they may be }
  91. {            called at any time , knowing that you have enough memory becomes important . Thus , }
  92. {            these routines return a value to indicate what happened .  If they return zero ,}
  93. {             then memory allocation failed . ( omh ) }
  94. {10 / 21 / 87 Added another parameter to SkelMenu:  drawBar: Boolean . This tells SkelMenu }
  95. {            whether to draw the menu bar after adding the Menu . This is done to eliminate }
  96. {            the menus popping up one at a time . Simply call SkelMenu  with drawBar false}
  97. {            until the    last time you call SkelMenu , then call it (for the last menu )}
  98. {            with drawBar true . ( omh ) }
  99. {10 / 26 / 87 Removed declarations for zoom - in and zoom - out . Added Pascal }
  100. {            changes ( above ) to C version . ( omh )}
  101.  
  102. {    02 / 02 / 88 Merged pd 's 1.04 changes with those of omh, above, to create}
  103. {            release version 2.0 . Fixed bug whereby cmd - key equivalents}
  104. {            for menu selections would execute twice if DA window in front . Thanks }
  105. {            to Don Fredkin and Julian Vrieslander for finding this one , and to Don for the}
  106. {             best fix . ( pd ) }
  107. { 10/28/88 Removed all New Rom calls. }
  108. { 10/28/88 Added support for conditional compilation for dialogs and MPW support.  By setting }
  109. {            the Think_pascal flag to false, TransSkel will run under MPW. Now correctly written for LSP 2.0}
  110.  
  111. {Some fixes done later by Ingemar Ragnemalm. Search for "Ingemar" fo find these.}
  112. {They include some bug fix and hiearcical menu support.}
  113.  
  114. {New fix 18/9-93: Added a filter proc for dialogs, to allow special event processing before}
  115. {an event is passed to IsDialogEvent, when a modeless dialog is in the front. This makes it possible}
  116. {to use user items in modeless dialogs, and to handle return/enter.}
  117.  
  118. unit TransSkel;
  119.  
  120. interface
  121.  
  122. {$SETC supportDialogs:= true }
  123.                     { Set to false to disallow modeless dialog support and save code space }
  124.                 { Set to false to have SkelInit call QuickDraw Inits: InitGraf, InitDialog, etc. }
  125.  
  126. {$IFC UNDEFINED THINK_PASCAL}
  127.  
  128.     uses
  129.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf;
  130. {$ENDC}
  131.  
  132.     procedure SkelInit (noMasters: integer; myGrowZone: ProcPtr);
  133.     procedure SkelMain;
  134.     procedure SkelWhoa;
  135.     procedure SkelClobber;
  136.     function SkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr; DrawBar: Boolean): Boolean;
  137.     function SkelHMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean; {Added by Ingemar 22/8 -93}
  138.     procedure SkelRmveMenu (theMenu: MenuHandle);
  139.     procedure SkelApple (aboutTitle: Str255; aboutProc: ProcPtr);
  140.     function SkelWindow (theWind: WindowPtr; pMouse, pKey, pUpdate, pActivate, pClose, pClobber, pIdle: ProcPtr; frontOnly: Boolean): Boolean;
  141.     procedure SkelRmveWind (theWind: WindowPtr);
  142. {$IFC supportDialogs }
  143.     function SkelDialog (theDialog: DialogPtr; pEvent, pClose, pClobber, pFilter: ProcPtr): Boolean; {pFilter added by Ingemar 18/9-93}
  144.     procedure SkelRmveDlog (theDialog: DialogPtr);
  145. {$ENDC}
  146.     procedure SkelGrowBounds (theWind: WindowPtr; hLO, vLo, hHi, vHi: integer);
  147.     procedure SkelEventMask (mask: integer);
  148.     procedure SkelGetEventMask (var mask: integer);
  149.     procedure SkelBackground (p: ProcPtr);
  150.     procedure SkelGetBackground (var p: ProcPtr);
  151.     procedure SkelEventHook (p: ProcPtr);
  152.     procedure SkelGetEventHook (var p: ProcPtr);
  153. {$IFC supportDialogs }
  154.     procedure SkelDlogMask (mask: integer);
  155.     procedure SkelGetDlogMask (var mask: integer);
  156. {$ENDC}
  157. {Two new procedures for WNE-support, added by Ingemar 12/11-93}
  158.     procedure SkelSetSleep (newSleep: Longint);
  159.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  160.  
  161.  
  162. implementation
  163.  
  164.     const
  165.         mBarHeight = 20;    { menu bar height.  All window sizing}
  166.  
  167.         GrowZoneSize = 4000;        {  Size of memory to be freed when GrowZone Proc called }
  168.  
  169.         defaultSleep = 5;        { Added by Ingemar dec -93. SkelInit uses this value for sleep time.}
  170.                             { The programmer can change it with SkelSetSleep }
  171.  
  172. {    This window zooming stuff may need to be removed if you use the new Rom libraries        }
  173. {    if not, then you can add zooming without the overhead of the new Rom libs.  See TrackBox    }
  174. {     routine also.                                                                                            }
  175.  
  176. {    Window and Menu handler types, constants, variables.}
  177.  
  178. {    whList and mhList are the lists of window and menu handlers.}
  179. {    whClobOnRmve and mhClobOnRmve are true if the handler disposal proc}
  180. {    is to be called when a handler is removed.  They are temporarily set}
  181. {    false when handlers are installed for windows or menus that already}
  182. {    have handlers - the old handler is removed WITHOUT calling the}
  183. {    disposal proc.}
  184.  
  185. {    Default lower limits on window sizing of 80 pixels both directions is}
  186. {    sufficient to allow text windows room to draw a grow box and scroll}
  187. {    bars without having the thumb and arrows overlap.  These values may}
  188. {    be changed if such a constraint is undesirable with SkelGrowBounds.}
  189. {    Default upper limits are for the Macintosh, not the Lisa, but are set}
  190. {    per machine in SkelInit.}
  191.  
  192.     type
  193.         WHandlerPtr = ^WHandler;
  194.         WHandlerHnd = ^WHandlerPtr;
  195.         WHandler = record
  196.                 whWind: WindowPtr;    {window/dialog to be handled    }
  197.                 whClobber: ProcPtr;    { data structure disposal proc    }
  198.                 whMouse: ProcPtr;        { mouse-click handler proc        }
  199.                 whKey: ProcPtr;        { key-click handler proc            }
  200.                 whUpdate: ProcPtr;        { update handler proc                }
  201.                 whActivate: ProcPtr;    { activate event handler proc    }
  202.                 whClose: ProcPtr;        { close "event" handler proc        }
  203.                 whIdle: ProcPtr;            { main loop proc                    }
  204. {$IFC supportDialogs }
  205.                 whEvent: ProcPtr;        { dialog event proc                }
  206.                 whFilter: ProcPtr;        { dialog filter proc ADDED BY INGEMAR 18/9 -93}
  207. {$ENDC }
  208.                 whHasGrow: Boolean;    { can window grow?                }
  209.                 whGrow: Rect;            { limits on window sizing        }
  210.                 whSized: Boolean;        { true = window was resized    }
  211.                 whFrontOnly: Boolean;    { true = idle only when active    }
  212.                 whNext: WHandlerHnd;            { next window handler            }
  213.             end;
  214.  
  215.         MHandlerPtr = ^MHandler;
  216.         MHandlerHnd = ^MHandlerPtr;
  217.  
  218.         MHandler = record
  219.                 mhID: integer;                { menu id                                    }
  220.                 mhSelect: ProcPtr;            { item selection handler proc            }
  221.                 mhClobber: ProcPtr;        { menu disposal handler proc            }
  222.                 mhNext: MHandlerHnd;        { next menu handler                        }
  223.             end;
  224.  
  225.     var
  226.         whList: WHandlerHnd;                { list of menu handlers }
  227.         whClobOnRmve: Boolean;
  228.         growRect: Rect;
  229.         mhList: MHandlerHnd;
  230.         mhClobOnRmve: Boolean;
  231.  
  232. {    Variables for default Apple menu handler.  appleID is set to 1 if}
  233. {    SkelApple is called and is the id of the Apple menu, appleAboutProc}
  234. {    is the procedure to execute if there is an About... item and it's}
  235. {    chosen from the Apple menu.  If doAbout is true, then the menu}
  236. {    contains the About... item, otherwise it's just desk accessories.}
  237.  
  238.         appleMenu: MenuHandle;
  239.         appleID: integer;
  240.         appleAboutProc: ProcPtr;
  241.         doAbout: Boolean;
  242.  
  243. {    Miscellaneous}
  244.  
  245. {    screenPort points to the window manager port.}
  246.  
  247. {    doneFlag determines when SkelMain returns.  It is set by calling}
  248. {    SkelWhoa(), which the host does to request a halt.}
  249.  
  250. {    pBkgnd points to a background procedure, to be run during event}
  251. {    processing.  Set it with SkelBackground.  If nil, there's no}
  252. {    procedure.}
  253.  
  254. {    pEvent points to an event-inspecting hook, to be run whenever an}
  255. {    event occurs.  Set it with SkelEventHook.  If nil, there's no}
  256. {    procedure.}
  257.  
  258. {    eventMask controls the event types requested in the GetNextEvent}
  259. {    call in SkelMain.}
  260.  
  261. {    diskInitPt is the location at which the disk initialization dialog}
  262. {    appears, if an uninitialized disk is inserted.}
  263.  
  264.         screenPort: GrafPtr;
  265.         doneFlag: integer;
  266.         pBkgnd: ProcPtr;
  267.         pEvent: ProcPtr;
  268.         eventMask: integer;
  269.         diskInitPt: Point;
  270.  
  271. {Added by Ingemar 12/11-93, for WNE-support:}
  272.         WNEImplemented: Boolean;
  273.         sleepTicks: Longint;
  274.         mouseRgn: rgnHandle;
  275.  
  276. {$IFC supportDialogs }
  277.  
  278. {    Events that are passed to dialogs.  Others are ignored.}
  279. {    Standard mask passes , mousedown, keydown, autokey, update,}
  280. {    activate and null events.  Null events are controlled by bit 0.}
  281.  
  282.         dlogEventMask: integer;
  283. {$ENDC}
  284.         pEventflag: Boolean;
  285.  
  286. {    "caching" global variables.  previous version would search down the window }
  287. {    list for every event it found.  Now, if the event happened to the same window }
  288. {    as last time, GetWDHandler will just do a simple compare }
  289. {    and return the last window handler.  This speeds up multiple window applications }
  290. {    immensely, at only a slight cost when you activate a new window (one }
  291. {    compare!)  If you don't like it, use the old version. }
  292.  
  293.         oldWindow: WindowPtr;
  294.         oldWDHandler: WHandlerHnd;
  295.  
  296. {    Global for built in "Grow Zone" function  }
  297.  
  298.         safetyHandle: Handle;
  299.  
  300.         myDitl: packed array[0..100] of byte;
  301.  
  302. { Rather than including the entire new ROM libraries, with all the other stuff you might not use    }
  303. { I've instead included just the Zoom box stuff here.  Depending on your status, you can either    }
  304. { leave things as they are, and only use zooming from the new Rom libs, or comment out the        }
  305. { calls, and include the new Rom libraries if you want to incorporate other new Rom calls        }
  306.  
  307.  
  308. { -------------------------------------------------------------------- }
  309. {                        Internal (private) Routines                                    }
  310. { -------------------------------------------------------------------- }
  311.  
  312.  
  313.  
  314. {    Get handler associated with user or dialog window.}
  315. {    Return nil if window doesn't belong to any known handler.}
  316. {    This routine is absolutely fundamental to TransSkel.}
  317.  
  318.     function GetWDHandler (theWind: WindowPtr): WHandlerHnd;
  319.  
  320.         var
  321.             h: WHandlerHnd;
  322.     begin
  323.         h := WhList;
  324.         GetWDHandler := nil;
  325.         if theWind = oldWindow then            {  caching code     }
  326.             GetWDHandler := oldWDHandler
  327.         else
  328.             while h <> nil do
  329.                 if h^^.whWind = theWind then
  330.                     begin
  331.                         oldWindow := theWind;            { Load in new values for new window }
  332.                         oldWDHandler := h;
  333.                         GetWDHandler := h;
  334.                         h := nil;
  335.                     end
  336.                 else
  337.                     h := WHandlerHnd(h^^.whNext);
  338.     end;
  339.  
  340. { Get Handler associated with user window.  Return nil if window doesn't}
  341. {  have a Handler. }
  342.  
  343.     function GetWHandler (theWind: WindowPtr): WHandlerHnd;
  344.  
  345.         var
  346.             h: WHandlerHnd;
  347.             myPeek: WindowPeek;
  348.  
  349.     begin
  350. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a dialog}
  351.         h := GetWDHandler(theWind);
  352.         myPeek := WindowPeek(theWind);
  353.         GetWHandler := nil; {default, moved up by Ingemar}
  354.         if h <> nil then
  355.             begin
  356.                 if mypeek^.windowKind <> dialogKind then
  357.                     GetWHandler := h;
  358.             end;
  359.     end;
  360.  
  361. {$IFC supportDialogs }
  362.  
  363. {    Get handler associated with dialog window.}
  364. {    Return nil if window doesn't belong to any known handler.}
  365.  
  366.     function GetDHandler (theDialog: WindowPtr): WHandlerHnd;
  367.  
  368.         var
  369.             h: WHandlerHnd;
  370.             myPeek: WindowPeek;
  371.  
  372.     begin
  373. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a non-dialog}
  374.         h := GetWDHandler(theDialog);
  375.         myPeek := WindowPeek(theDialog);
  376.         GetDHandler := nil; {default - moved up by Ingemar}
  377.         if h <> nil then
  378.             begin
  379.                 if mypeek^.windowKind = dialogKind then
  380.                     GetDHandler := h;
  381.             end;
  382.     end;
  383. {$ENDC}
  384.  
  385. {Two new routines added by Ingemar 12/11-93 for WNE support:}
  386.     procedure SkelSetSleep (newSleep: Longint);
  387.     begin
  388.         sleepTicks := newSleep;
  389.     end;
  390.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  391.     begin
  392.         mouseRgn := newMouseRgn;
  393.     end;
  394.  
  395. {The following procedures are Pascal "glue" that allows Pascal to call a Procedure    }
  396. { from a ProcPtr.  It is similar to (*p) () construct used in the C dialect.  Different    }
  397. { procedures are necessary for the reason of Pascal's strongly typed parameter        }
  398. { list.  Fortunately, there are not too many calls which use different param lists        }
  399.  
  400.     procedure callpMouse (thePoint: Point; theTime: longint; theMods: integer; myProc: ProcPtr);
  401.  
  402.     inline
  403.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  404.         $4e90;
  405.  
  406.     procedure callpKey (theChar: char; theMods: integer; myProc: ProcPtr);
  407.  
  408.     inline
  409.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4( be necessary to set the port, as the  click is passed to the}
  410. {    active window's handler. }
  411.  
  412.     procedure DoKey (h: WHandlerHnd; ch: char; mods: integer);
  413.         var
  414.             p: ProcPtr;
  415.  
  416.     begin
  417.         if h <> nil then
  418.             begin
  419.                 p := h^^.whKey;
  420.                 if p <> nil then
  421.                     callpKey(ch, mods, p);
  422.             end;
  423.     end;
  424.  
  425. {    Call the window updating procedure, passing to it an indicator whether the}
  426. {    window has been resized or not.  Then clear the flag, assuming the update}
  427. {    proc took whatever action was necessary to respond to resizing.}
  428. {}
  429. {    If the handler doesn't have any update proc, the Begin/EndUpdate stuff}
  430. {    is still done, to clear the update region.  Otherwise the Window Manager }
  431. {    will keep generating update events for the window, stalling updates of}
  432. {    other windows.    }
  433.  
  434. {    Make sure to save and restore the port, as it's not always the active window}
  435. {    that's updated.    }
  436.  
  437.     procedure DoUpdate (h: WHandlerHnd);
  438.  
  439.         var
  440.             rh: WhandlerHnd;
  441.             p: ProcPtr;
  442.             updPort, tmpPort: GrafPtr;
  443.  
  444.     begin
  445.         rh := h;
  446.         if rh <> nil then
  447.             begin
  448.                 GetPort(tmpPort);
  449.                 updPort := rh^^.whWind;
  450.                 SetPort(updPort);
  451.                 BeginUpdate(updPort);
  452.                 p := rh^^.whUpdate;
  453.                 if p <> nil then
  454.                     begin
  455.                         callpBoolean(rh^^.whSized, p);
  456.                         rh^^.whSized := false;
  457.                     end;
  458.                 EndUpdate(updPort);
  459.                 SetPort(tmpPort);
  460.             end;
  461.     end;
  462.  
  463. {    Pass activate/deactivate notification to handler.  On activate, set the port to}
  464. {    the window coming active    }
  465.  
  466.     procedure DoActivate (h: WHandlerHnd; active: Boolean);
  467.  
  468.         var
  469.             p: ProcPtr;
  470.  
  471.     begin
  472.         if h <> nil then
  473.             begin
  474.                 if active then
  475.                     SetPort(h^^.whWind);
  476.                 p := h^^.whActivate;
  477.                 if p <> nil then
  478.                     callpBoolean(active, p);
  479.             end
  480.     end;
  481.  
  482. {    Execute a window handler's close proc.  The close box for handlers}
  483. {    for temp windows that want to remove themselves when the window}
  484. {    is closed can call SkelRmveWind to dispose of the window}
  485. {    and remove the handler from the window handler list.  Thus, windows}
  486. {    may be dynamically created and destroyed without filling up the}
  487. {    handler list with a bunch of invalid handlers.}
  488.  
  489. {    If the handler doesn't have a close proc, just hide the window.}
  490. {    The host should provide some way of reopening the window (perhaps}
  491. {    a menu selection).  Otherwise the window will be lost from user}
  492. {    control if it is hidden, since it won't receive user-initiated events.}
  493.  
  494. {    Since the close box of only the active window may be clicked, it}
  495. {    is not necessary to set the port . }
  496.  
  497. {    This is called both for regular and dialog windows.}
  498.  
  499.     procedure DoClose (h: WHandlerHnd);
  500.  
  501.         var
  502.             rh: WHandlerHnd;
  503.             p: ProcPtr;
  504.     begin
  505.         rh := h;
  506.         if rh <> nil then
  507.             begin
  508.                 p := rh^^.whClose;
  509.                 if (p <> nil) then
  510.                     callpnoarg(p)
  511.                 else
  512.                     HideWindow(rh^^.whWind);
  513.             end;
  514.     end;
  515.  
  516. {    Execute a window Handler's clobber proc.  This is called both for regular and dialog windows.}
  517. {    Must save, set and restore port, since any window (not just active one) may be clobbered    }
  518. {    at any time.}
  519. {}
  520. {    Don't need to check whether handler is nil, as in other handler procedures, since this is only}
  521. {    called by SkelRmveWind with a known valid handler.     }
  522.  
  523.     procedure DoClobber (h: WHandlerHnd);
  524.  
  525.         var
  526.             p: ProcPtr;
  527.             curPort: Grafptr;
  528.     begin
  529.         if (h <> nil) then
  530.             begin
  531.                 GetPort(curPort);
  532.                 SetPort(h^^.whWind);
  533.                 p := h^^.whClobber;
  534.                 if p <> nil then
  535.                     callpnoarg(p);
  536.                 SetPort(curPort);
  537.             end;
  538.     end;
  539.  
  540. {$IFC supportDialogs }
  541.  
  542. {    Handle event if it's for a dialog.  The event must be one of}
  543. {    those that is passed to dialogs according to dlogEventMask.}
  544. {    This mask can be set so that disk-inserts, for instance, don't}
  545. {    get eaten up.}
  546.  
  547.     function DoDialog (theEvent: EventRecord): Boolean;
  548.  
  549.         var
  550.             dh: WHandlerHnd;
  551.             theDialog: DialogPtr;
  552.             myDPeek: DialogPeek;
  553.             what: integer;
  554.             item: integer;
  555.             tmpPort: GrafPtr;
  556.             ignore: Boolean;
  557.             testme: longint;
  558.             hasfilter, filtered: Boolean;
  559.  
  560.     begin
  561.  
  562. {    handle command keys before they get to IsDialogEvent}
  563.  
  564.         what := theEvent.what;
  565.         testme := BitShift(longint(1), what);
  566.         testme := BitAnd(testme, longint(dlogEventMask));
  567.         if (((what = keydown) or (what = autokey)) and (BitAnd(theEvent.modifiers, cmdkey) <> 0)) then
  568. { Bugfix by Ingemar above! }
  569.             begin
  570.                 DoMenuCommand(MenuKey(Char(BitAnd(theEvent.message, charCodeMask))));
  571.                 DoDialog := true;
  572.             end
  573. {Filter procedure, Added by Ingemar 18/9 -93:}
  574.         else
  575.             begin
  576. {Kolla om FrontWindow har whFilter!}
  577.                 dh := WHandlerHnd(GetDHandler(FrontWindow));
  578.                 filtered := false;
  579.                 hasfilter := dh <> nil;
  580.                 if hasfilter then
  581.                     hasfilter := dh^^.whFilter <> nil;
  582.                 if hasfilter then
  583.                     callpfilter(FrontWindow, theEvent, filtered, dh^^.whFilter);
  584.                 DoDialog := filtered;
  585.                 if not filtered then
  586. {end of filter proc handling}
  587. {else}
  588.                     if testme > 0 then
  589.                         if IsDialogEvent(theEvent) then
  590.                             begin
  591.                                 if DialogSelect(theEvent, theDialog, item) then
  592.                                     begin
  593.                                         dh := WHandlerHnd(GetDHandler(theDialog));
  594.                                         if (dh <> nil) then
  595.                                             if (dh^^.whEvent <> nil) then
  596.                                                 begin
  597.                                                     GetPort(tmpPort);
  598.                                                     SetPort(theDialog);
  599.                                                     callpEvent(item, theEvent, dh^^.whEvent);
  600.                                                     SetPort(tmpPort);
  601.                                                 end;
  602.                                     end;
  603.                                 DoDialog := true;
  604.                             end
  605.                         else
  606.                             DoDialog := false;
  607.             end; {to match begin added with filter above}
  608.     end;
  609. {$ENDC}
  610.  
  611. { -------------------------------------------------------------------- }
  612. {                            Event-handling routines                        }
  613. { -------------------------------------------------------------------- }
  614.  
  615. {    Have either sized or zoomed the window.  Invalidate it to force}
  616. {    an update and set the 'resized' flag in the window handler true.}
  617. {    The port is assumed to be set to the port that changed size.        }
  618.  
  619.     procedure TriggerUpdate (h: WHandlerHnd; thePort: GrafPtr);
  620.  
  621.     begin
  622.         InvalRgn(thePort^.visRgn);
  623. {Changed by Ingemar 1/4-94. Was: InvalRect(thePort^.portRect);}
  624. {This is ok for rectangular windows, but all windows aren't rectangular!}
  625.         if (h <> nil) then
  626.             begin
  627.                 h^^.whSized := true;
  628.             end;
  629.     end;
  630.  
  631. {    Size a window.  If the window has a handler, use the grow limits}
  632. {    in the handler record, otherwise use the defaults.}
  633.  
  634. {    The portRect is invalidated to force an update event.    The handler's}
  635. {    update procedure should check the parameter passed to it to check}
  636. {    whether the window has changed size, if it needs to adjust itself to}
  637. {    the new size.  THIS IS A CONVENTION.  Update procs must notice grow}
  638. {    "events", there is no procedure specifically for such events.}
  639.  
  640. {    The clipping rectangle is not reset.  If the host application}
  641. {    keeps the clipping set equal to the portRect or something similar,}
  642. {    then it will have to arrange to treat window growing with more}
  643. {    care.}
  644. {}
  645. {    Since the grow region of only the active window may be clicked, it should}
  646. {    not be necessary to set the port.}
  647.  
  648.     procedure DoGrow (h: WHandlerHnd; thePort: GrafPtr; StartPt: Point);
  649.  
  650.         var
  651.             r: Rect;
  652.             growRes: longint;
  653.  
  654.     begin
  655.         if (h <> nil) then
  656.             begin
  657.                 r := h^^.whGrow;
  658.             end
  659.         else
  660.             r := growRect;
  661.         growRes := GrowWindow(thePort, startPt, r);
  662.         if growRes <> 0 then
  663.             begin
  664.                 SizeWindow(thePort, LoWord(growRes), HiWord(growRes), false);
  665.                 TriggerUpdate(h, thePort);
  666.             end;
  667.     end;
  668.  
  669.  
  670. {    Zoom the current window.  Very similar to DoGrow}
  671. {    Since the zoombox of only the active window may be clicked, it should not be necessary}
  672. {    to set the port.    }
  673.  
  674.     procedure DoZoom (h: WHandlerHnd; thePort: GrafPtr; partcode: integer);
  675.  
  676.     begin
  677.         ZoomWindow(thePort, partcode, false);
  678.         TriggerUpdate(h, thePort);
  679.     end;
  680.  
  681. {    General event handler}
  682.  
  683.     procedure DoEvent (theEvt: Eventrecord);
  684.  
  685.         var
  686.             theEvent: EventRecord;
  687.             evtPt: Point;
  688.             evtPort: GrafPtr;
  689.             evtPart: integer;
  690.             evtChar: char;
  691.             evtMods: integer;
  692.             h: WHandlerHnd;
  693.             r: Rect;
  694.             ignore: integer;
  695.  
  696.     begin
  697.         theEvent := theEvt;
  698. {$IFC supportDialogs }
  699.         if not (DoDialog(theEvent)) then
  700. {$ENDC}
  701.             begin
  702.                 evtPt := theEvent.where;
  703.                 case theEvent.what of
  704.                     nullEvent: 
  705.                         ;
  706.  
  707. {    Mouse click.  Get the window that the click occurred in, and the}
  708. {    part of the window.    Get WDHandler is called here, not GetWHandler, since}
  709. {    we need the handler for a window which might turn out to be a dialog window,}
  710. {    e.g., if the click is in a close box.}
  711.  
  712.                     mouseDown: 
  713.                         begin
  714.                             evtPart := FindWindow(evtPt, evtPort);
  715.                             h := GetWDHandler(evtPort);
  716.                             case evtPart of
  717.  
  718. {    Click in a desk accessory window.  Pass back to the system.}
  719.  
  720.                                 inSysWindow: 
  721.                                     SystemClick(theEvent, evtPort);
  722.  
  723. {    Click in menu bar.  Track the mouse and execute selected command,}
  724. {    if any.}
  725.  
  726.                                 inMenuBar: 
  727.                                     DoMenuCommand(MenuSelect(evtPt));
  728.  
  729. {    Click in grow box.  Resize window.}
  730.  
  731.                                 inGrow: 
  732.                                     DoGrow(h, evtPort, evtPt);
  733.  
  734. {    Click in title bar.  Drag the window around.  Leave at least}
  735. {    4 pixels visible in both directions.    Bug fix: The window, if not front, is}
  736. {    selected first to make sure it's at least activated (unless the command key is down - see Inside}
  737. {    Macintosh).  DragWindow seems to call StillDown first, so that clicks in drag regions while}
  738. {    machine is busy don't otherwise bring window to front if the mouse is already up by the time}
  739. {    DragWindow is called.}
  740.  
  741. {BUG??? Where is evtmods assigned???}
  742.  
  743.                                 inDrag: 
  744.                                     begin
  745.                                         if (evtPort <> FrontWindow) and (BitAnd(evtmods, cmdKey) = 0) then
  746.                                             SelectWindow(evtPort);
  747.                                         r := screenPort^.portRect;
  748.                                         r.top := r.top + mBarHeight;        { Skip down past menu bar    }
  749.                                         InsetRect(r, 4, 4);
  750.                                         DragWindow(evtPort, evtPt, r);
  751.                                     end;
  752.  
  753. {    Click in close box.  Call the close proc if the window has one.}
  754.  
  755.                                 inGoAway: 
  756.                                     if (TrackGoAway(evtPort, evtPt)) then
  757.                                         DoClose(GetWDHandler(evtPort));
  758.  
  759. {    Click in content region.  If the window wasn't frontmost (active),}
  760. {    just select it, otherwise pass the click to the window's mouse}
  761. {    click handler.}
  762.  
  763.                                 inContent: 
  764.                                     if (evtPort <> FrontWindow) then
  765.                                         SelectWindow(evtPort)
  766.                                     else
  767.                                         DoMouse(h, theEvent);
  768.  
  769. {    Click in zoom box.  Track the click and then zoom the window if}
  770. {    necessary}
  771.  
  772.                                 inZoomin, inZoomOut: 
  773.                                     if (TrackBox(evtPort, evtPt, evtPart)) then
  774.                                         DoZoom(h, evtport, evtPart);
  775.                                 otherwise
  776.                                     ;
  777.                             end;{mousedown}
  778.                         end;
  779.  
  780. {    Key event.  If the command key was down, process as menu item}
  781. {    selection, otherwise pass the character and the modifiers flags}
  782. {    to the active window's key handler.}
  783.  
  784. {    If dialogs are supported, there's no check for command-key}
  785. {    equivalents, since that would have been checked in DoDialog.}
  786.  
  787.                     keydown, autokey: 
  788.                         begin
  789.                             evtChar := char(BitAnd(theEvent.message, charCodeMask));
  790.                             evtMods := theEvent.modifiers;
  791.                             if BitAnd(evtMods, cmdKey) > 0 then
  792.                                 DoMenuCommand(menuKey(evtChar))
  793.                             else
  794.                                 DoKey(GetWHandler(FrontWindow), evtChar, evtMods);
  795.                         end;
  796.  
  797. {    Update a window.}
  798.  
  799.                     updateEvt: 
  800.                         DoUpdate(GetWHandler(WindowPtr(theEvent.message)));
  801.  
  802. {    Activate or deactivate a window.}
  803.  
  804.                     activateEvt: 
  805.                         DoActivate(GetWHandler(WindowPtr(theEvent.message)), (BitAnd(theEvent.modifiers, activeFlag) <> 0));
  806.  
  807. {    handle inserts of uninitialized disks}
  808.  
  809.                     diskEvt: 
  810.                         if (HiWord(theEvent.message) <> noErr) then
  811.                             begin
  812.                                 DILoad;
  813.                                 ignore := DIBadMount(diskInitPt, theEvent.message);
  814.                                 DIUnload;
  815.                             end;
  816.                     otherwise
  817.                 end;
  818.             end;
  819.     end;
  820.  
  821. { -------------------------------------------------------------------- }
  822. {                        Interface (public) Routines                        }
  823. { -------------------------------------------------------------------- }
  824.  
  825.  
  826. {    Initialize the various Macintosh Managers.}
  827. {    Set default upper limits on window sizing.}
  828. {    FlushEvents does NOT toss disk insert events, so that disks}
  829. {    inserted while the application is starting up don't result}
  830. {    in dead drives.}
  831. {    NoMasters is the number of times to call MoreMasters.  gzProc is the address of a user - provided}
  832. {    grow zone function procedure to call if memory gets tight.  Pass nil if none to be used.    }
  833.  
  834.     procedure SkelInit;
  835.         var
  836.             i: integer;
  837.  
  838.     begin
  839.  
  840. { For non-Lightspeed Pascal users, the following inits are included as a compile time option, }
  841. {  See the $SETC definition at the beginning of the unit.  }
  842.  
  843. {$IFC UNDEFINED THINK_PASCAL }
  844.  
  845.         InitGraf(@thePort);
  846.         InitFonts;
  847.         InitWindows;
  848.         InitMenus;
  849.         TEInit;
  850.         InitDialogs(nil);
  851.         MaxApplZone;
  852. {$ENDC}
  853.  
  854.         FlushEvents(everyEvent - diskMask, 0);
  855.         for i := 1 to noMasters do
  856.             MoreMasters;
  857.         if myGrowZone <> nil then
  858.             SetGrowZone(myGrowZone);
  859.  
  860.         InitCursor;
  861.  
  862. {Added by Ingemar 12/11-93, for WNE-support:}
  863.         WNEImplemented := NGetTrapAddress($60, ToolTrap) <> NgetTrapAddress($9F, ToolTrap);
  864.         SkelSetSleep(defaultSleep);
  865.         SkelSetMouseRgn(nil);
  866. {End WNE-support}
  867.  
  868.         whList := nil;
  869.         whClobOnRmve := true;
  870.         SetRect(growRect, 80, 80, 512, 342 - mBarHeight);
  871.         mhList := nil;
  872.         mhClobOnRmve := true;
  873.         appleID := 0;
  874.         appleAboutProc := nil;
  875.         doAbout := false;
  876.         doneflag := 0;
  877.         pBkgnd := nil;
  878.         pEvent := nil;
  879.         pEventflag := false;
  880.         eventmask := everyEvent;
  881.         diskInitPt.v := 120;
  882.         diskInitPt.h := 100;
  883. {$IFC supportDialogs }
  884.         dlogEventMask := $16f;
  885. {$ENDC}
  886.  
  887. {    Set upper limits of window sizing to machine screen size.  Allow}
  888. {    for the menu bar.}
  889.  
  890.         GetWMgrPort(screenport);
  891.         growRect.right := screenPort^.portRect.right;
  892.         growRect.bottom := screenPort^.portRect.bottom - mBarHeight;
  893.  
  894. {    Set caching global variables to nil }
  895.  
  896.         oldWindow := nil;
  897.         oldWDHandler := nil;
  898.     end;
  899.  
  900. {    Main loop.}
  901.  
  902. {    Task care of DA's with SystemTask.}
  903. {    Run background task if there is one.}
  904. {    If there is an event, check for an event hook.  If there isn't}
  905. {    one defined, or if there is but it returns false, call the}
  906. {    general event handler.  (Hook returns true if TransSkel should}
  907. {    ignore the event.)}
  908. {    If no event, call the "no-event" handler for the front window and for}
  909. {    any other windows with idle procedures that are always supposed}
  910. {    to run.  This is done in such a way that it is safe for idle procs}
  911. {    to remove the handler for their own window if they want (unlikely,}
  912. {    but...)  This loop doesn't check whether the window is really}
  913. {    a dialog window or not, but it doesn't have to, because such}
  914. {    things always have a nil idle proc.}
  915. {    }
  916. {    doneFlag is reset upon exit.  This allows it to be called}
  917. {    repeatedly, or recursively.}
  918.  
  919. {    Null events are looked at (in SkelMain)}
  920. {    and passed to the event handler.  This is necessary to make sure}
  921. {    DialogSelect gets called repeatedly, or the caret won't blink if}
  922. {    a dialog has any editText items.  Null events are not passed to any event-inspecting hook that may}
  923. {    be installed.}
  924.  
  925.     procedure SkelMain;
  926.  
  927.         var
  928.             theEvent: EventRecord;
  929.             wh, wh2: WHandlerHnd;
  930.             w: WindowPtr;
  931.             haveEvent, testpevent, testbool: Boolean;
  932.             tmpPort: GrafPtr;
  933.             p: ProcPtr;
  934.  
  935.     begin
  936.         while (doneFlag = 0) do
  937.             begin
  938.                 if WNEImplemented then {Added by Ingemar 12/11-93, for WNE-support:}
  939.                     begin
  940.                         if (pBkgnd <> nil) then
  941.                             callpnoarg(pBkgnd);
  942.                         haveEvent := WaitNextEvent(eventMask, theEvent, sleepTicks, mouseRgn);
  943.                     end
  944.                 else
  945.                     begin {Old way:}
  946.                         SystemTask;
  947.                         if (pBkgnd <> nil) then
  948.                             callpnoarg(pBkgnd);
  949.                         haveEvent := GetNextEvent(eventMask, theEvent);
  950.                     end;
  951.  
  952.                 if (pEvent <> nil) then
  953.                     testpevent := CallotherEvent(theEvent, pEvent)
  954.                 else
  955.                     testpevent := false;
  956. { following line fixed from version 1.02 & 1.03     [but was still buggy! /Ingemar]}
  957. {$IFC supportDialogs }
  958.                 if (pEvent = nil) or (testpevent = false) then {haveEvent and <- This is wrong for modeless dialogs according to page 416? Changed by Ingemar 6/8 -93}
  959. {$ELSEC}
  960.                     if haveEvent and ((pEvent = nil) or (testpevent = false)) then { Old line. IFC'ad by Ingemar 6/8 -93}
  961. {$ENDC}
  962.                         DoEvent(theEvent);
  963.                 if not haveEvent then
  964.                     begin
  965.                         wh := whList;
  966.                         GetPort(tmpPort);
  967.                         while (wh <> nil) do
  968.                             begin
  969.                                 wh2 := wh^^.whNext;
  970.                                 w := wh^^.whWind;
  971.                                 if ((w = FrontWindow) or not wh^^.whFrontOnly) then
  972.                                     begin
  973.                                         SystemTask;
  974.                                         if (wh^^.whIdle <> nil) then
  975.                                             begin
  976.                                                 SetPort(wh^^.whWind);
  977.                                                 p := wh^^.whIdle;
  978.                                                 if (p <> nil) then
  979.                                                     callpnoarg(p);
  980.                                             end;
  981.                                     end;
  982.                                 wh := wh2;
  983.                             end;
  984.                         SetPort(tmpPort);
  985.                     end;
  986.             end;
  987.         doneFlag := 0;
  988.     end;
  989.  
  990. {    Tell SkelMain to stop}
  991.  
  992.     procedure SkelWhoa;
  993.     begin
  994.         doneFlag := 1;
  995.     end;
  996.  
  997. {    Clobber all the menu, window and dialog handlers}
  998.  
  999.     procedure SkelClobber;
  1000.  
  1001.  
  1002.     begin
  1003.         oldWDHandler := nil;
  1004.         oldWindow := nil;
  1005.         while (whList <> nil) do
  1006.             begin
  1007.                 SkelRmveWind(whList^^.whWind);
  1008.             end;
  1009.         while (mhList <> nil) do
  1010.             begin
  1011.                 SkelRmveMenu(GetMHandle(mhList^^.mhID));
  1012.             end;
  1013.     end;
  1014.  
  1015. { -------------------------------------------------------------------- }
  1016. {                        Menu-handler interface routines                            }
  1017. { -------------------------------------------------------------------- }
  1018.  
  1019.  
  1020.  
  1021.  
  1022. {    Install handler for a menu.  Remove any previous handler for it.}
  1023. {    Pass the following parameters:}
  1024.  
  1025. {    theMenu    Handle to the menu to be handled.  Must be created by host.}
  1026. {    pSelect    Proc that handles selection of items from menu.  If this is}
  1027. {            nil, the menu is installed, but nothing happens when items}
  1028. {            are selected from it.}
  1029. {    pClobber Proc for disposal of handler's data structures.  Usually}
  1030. {            nil for menus that remain in menu bar until program}
  1031. {            termination.}
  1032.  
  1033. {    The menu is installed and drawn in the menu bar.}
  1034.  
  1035. {     Return false if no handler could be allocated, true if successful. }
  1036.  
  1037.     function CommonSkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean;
  1038.         var
  1039.             mh: MHandlerHnd;
  1040.             myHand: Handle;
  1041.     begin
  1042.         mhClobOnRmve := false;
  1043.         SkelRmveMenu(theMenu);
  1044.         mhClobOnRmve := true;
  1045.         myHand := NewHandle(Sizeof(MHandler));
  1046.         CommonSkelMenu := false;
  1047.         if myHand <> nil then
  1048.             begin
  1049.                 CommonSkelMenu := true;                    { show we really got the memory }
  1050.                 mh := MHandlerHnd(myHand);
  1051.                 mh^^.mhNext := mhList;
  1052.                 mhList := MHandlerHnd(myHand);
  1053.                 mh^^.mhID := theMenu^^.menuID;    { get menu id number }
  1054.                 mh^^.mhSelect := pSelect;            { install selection handler }
  1055.                 mh^^.mhClobber := pClobber;        { install disposal handler }
  1056.             end;
  1057.     end;
  1058.  
  1059. {  Install handler for a normal menu }
  1060.  
  1061.     function SkelMenu;
  1062.         var
  1063.             success: Boolean;
  1064.     begin
  1065.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1066.         SkelMenu := success;
  1067.         if success then
  1068.             begin
  1069.                 InsertMenu(theMenu, 0);            { put menu at end of menu bar }
  1070.                 if DrawBar then
  1071.                     DrawMenuBar;
  1072.             end;
  1073.     end;
  1074.  
  1075. {    Install handler for a hiearcical menu.  Almost same as above.}
  1076. {    Added by Ingemar 22/8 -93}
  1077.  
  1078.     function SkelHMenu;
  1079.         var
  1080.             success: Boolean;
  1081.     begin
  1082.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1083.         SkelHMenu := success;
  1084.         if success then
  1085.             begin
  1086.                 InsertMenu(theMenu, -1);            { put menu at end of menu bar }
  1087.             end;
  1088.     end;
  1089.  
  1090.  
  1091. {    Remove a menu handler.  This calls the handler's disposal routine}
  1092. {    and then takes the handler out of the handler list and disposes}
  1093. {    of it.}
  1094.  
  1095. {    Note that the menu MUST be deleted from the menu bar before calling}
  1096. {    the clobber proc, because the menu bar will end up filled with}
  1097. {    garbage if the menu was allocated with NewMenu (see discussion of}
  1098. {    DisposeMenu in Menu Manager section of Inside Macintosh).}
  1099.  
  1100.     procedure SkelRmveMenu;
  1101.  
  1102.         var
  1103.             mID: integer;
  1104.             h, h2: MHandlerHnd;
  1105.             p: ProcPtr;
  1106.             returnflag: Boolean;
  1107.  
  1108.     begin
  1109.         mID := theMenu^^.menuID;
  1110.         returnflag := false;
  1111.         if mhlist <> nil then
  1112.             begin
  1113.                 if mhList^^.mhID = mID then
  1114.                     begin
  1115.                         h2 := mhlist;
  1116.                         mhList := h2^^.mhNext;
  1117.                     end
  1118.                 else
  1119.                     begin
  1120.                         h := mhList;
  1121.                         while (h <> nil) and not returnflag do
  1122.                             begin
  1123.                                 h2 := h^^.mhNext;
  1124.                                 if (h2 = nil) then
  1125.                                     begin
  1126.                                         h := nil;
  1127.                                         returnflag := true;
  1128.                                     end
  1129.                                 else if h2^^.mhID = mID then
  1130.                                     begin
  1131.                                         h^^.mhNext := h2^^.mhNext;
  1132.                                         h := nil;
  1133.                                     end;
  1134.                                 if h <> nil then
  1135.                                     h := h2;
  1136.                             end;
  1137.                     end;
  1138.                 if not returnflag then
  1139.                     begin
  1140.                         DeleteMenu(mID);
  1141.                         DrawMenuBar;
  1142.                         p := h2^^.mhClobber;
  1143.                         if mhClobOnRmve and (p <> nil) then
  1144.                             callpMenu(theMenu, p);
  1145.                         DisposHandle(Handle(h2));
  1146.                     end;
  1147.             end;
  1148.     end;
  1149.  
  1150. {    Install a handler for the Apple menu.}
  1151.  
  1152. {    SkelApple is called if TransSkel is supposed to handle the apple}
  1153. {    menu itself.  The title is the title of the first item.  If nil,}
  1154. {    then only desk accessories are put into the menu.  If not nil, then}
  1155. {    the title is entered as the first item, followed by a gray line,}
  1156. {    then the desk accessories.}
  1157.  
  1158. {     SkelApple does not cause the menubar to be drawn, so if the Apple menu is the only menu, }
  1159. {    DrawMenuBar must be called afterward.}
  1160.  
  1161. {    No value is returned, unlike SkelMenu.  It is assumed that SkelApple will be called so early in the}
  1162. {    application that the call to SkelMenu is virtually certain to succeed.  }
  1163.  
  1164.     procedure SkelApple;
  1165.  
  1166.         var
  1167.             appleTitle: Str255;
  1168.             dummy: boolean;
  1169.     begin
  1170.         appleTitle := ' ';
  1171.         appleTitle[1] := char($14);
  1172.         appleID := 1;
  1173.         AppleMenu := NewMenu(appleID, appleTitle);
  1174.         if aboutTitle <> '' then
  1175.             begin
  1176.                 doAbout := true;
  1177.                 AppendMenu(appleMenu, aboutTitle);
  1178.                 AppendMenu(appleMenu, '(-');
  1179.                 AppleAboutProc := aboutProc;
  1180.             end;
  1181.         AddResMenu(appleMenu, 'DRVR');
  1182.         dummy := SkelMenu(appleMenu, @DoAppleItem, @DoAppleClobber, false);
  1183.     end;
  1184.  
  1185. { -------------------------------------------------------------------- }
  1186. {                    Window-handler interface routines                    }
  1187. { -------------------------------------------------------------------- }
  1188.  
  1189.  
  1190.  
  1191. {    Install handler for a window.  Remove any previous handler for it.}
  1192. {    Pass the following parameters:}
  1193.  
  1194. {    theWind    Pointer to the window to be handled.  Must be created by host.}
  1195. {    pMouse    Proc to handle mouse clicks in window.  The proc will be}
  1196. {            passed the point (in local coordinates), the time of the}
  1197. {            click, and the modifier flags word.}
  1198. {    pKey    Proc to handle key clicks in window.  The proc will be passed}
  1199. {            the character and the modifier flags word.}
  1200. {    pUpdate    Proc for updating window.  TransSkel brackets calls to update}
  1201. {            procs with calls to BeginUpdate and EndUpdate, so the visRgn}
  1202. {            is set up correctly.  A flag is passed indicating whether the}
  1203. {            window was resized or not.  BY CONVENTION, the entire portRect}
  1204. {            is invalidated when the window is resized.  That way, the}
  1205. {            handler's update proc can redraw the entire content region}
  1206. {            without interference from BeginUpdate/EndUpdate.  The flag}
  1207. {            is set to false after the update proc is called; the}
  1208. {            assumption is made that it will notice the resizing and}
  1209. {            respond appropriately.}
  1210. {    pActivate Proc to execute when window is activated or deactivated.}
  1211. {            A boolean is passed to it which is true if the window is}
  1212. {            coming active, false if it's going inactive.}
  1213. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1214. {            mainly to temp window handlers that want to know when to}
  1215. {            self-destruct (with SkelRmveWind).}
  1216. {    pClobber Proc for disposal of handler's data structures}
  1217. {    pIdle    Proc to execute when no events are pending.}
  1218. {    frontOnly True if pIdle should execute on no events only when}
  1219. {            theWind is frontmost, false if executes all the time.  Note}
  1220. {            that if it always goes, everything else may be slowed down!}
  1221.  
  1222. {    If a particular procedure is not needed (e.g., key events are}
  1223. {    not processed by a handler), pass nil in place of the appropriate}
  1224. {    procedure address.}
  1225.  
  1226. {    Return false  if no handler could be allocated, true if successful.}
  1227.  
  1228.     function SkelWindow;
  1229.  
  1230.         var
  1231.             hHand: WhandlerHnd;
  1232.  
  1233.     begin
  1234.         whClobOnRmve := false;
  1235.         SkelRmveWind(theWind);
  1236.         whClobOnRmve := true;
  1237.  
  1238. {    Get new handler, attach to list of handlers.  It is attached to the beginning of the list, which is simpler;}
  1239. {    the order should be irrelevant to the hose, anyway. }
  1240.  
  1241.         hHand := WHandlerHnd(NewHandle(Sizeof(WHandler)));
  1242.         SkelWindow := false;
  1243.         if hHand <> nil then
  1244.             begin
  1245.                 hHand^^.whNext := whList;
  1246.                 whList := hHand;
  1247.                 with hHand^^ do
  1248.                     begin
  1249.                         SkelWindow := true;                    { Show that we got the memory }
  1250.                         whWind := theWind;
  1251.                         whMouse := pMouse;
  1252.                         whKey := pKey;
  1253.                         whUpdate := pUpdate;
  1254.                         whActivate := pActivate;
  1255.                         whClose := pClose;
  1256.                         whClobber := pClobber;
  1257.                         whIdle := pIdle;
  1258.                         whFrontOnly := frontOnly;
  1259.                         whSized := false;
  1260.                         whGrow := GrowRect;
  1261.                     end;
  1262.             end;
  1263.         SetPort(theWind); {Is this allowed for hidden windows? I thought so, but… /Ingemar, dec 93}
  1264.     end;
  1265.  
  1266. {    Remove a window handler.  This calls the handler's disposal routine}
  1267. {    and then takes the handler out of the handler list and disposes}
  1268. {    of it.}
  1269.  
  1270. {    SkelRmveWind is also called by SkelRmveDlog.}
  1271.  
  1272. {    Note that if the window cache variable is set to the window whose handler is being clobbered, the }
  1273. {    variable must be zeroed.    }
  1274.  
  1275.     procedure SkelRmveWind;
  1276.  
  1277.         var
  1278.             h, h2: WHandlerHnd;
  1279.             returnflag: Boolean;
  1280.  
  1281.     begin
  1282.         if theWind = oldWindow then
  1283.             begin
  1284.                 oldWindow := nil;
  1285. {•    oldWDHandler := nil;•}
  1286.             end;
  1287.  
  1288.         if (whList <> nil) then
  1289.             begin
  1290.                 returnflag := false;
  1291.                 if whList^^.whWind = theWind then
  1292.                     begin
  1293.                         h2 := whlist;
  1294.                         whList := whList^^.whNext;
  1295.                     end
  1296.                 else
  1297.                     begin
  1298.                         h := whList;
  1299.                         while (h <> nil) and not returnflag do
  1300.                             begin
  1301.                                 h2 := h^^.whNext;
  1302.                                 if (h2 = nil) then
  1303.                                     begin
  1304.                                         h := nil;
  1305.                                         returnflag := true;
  1306.                                     end
  1307.                                 else if h2^^.whWind = theWind then
  1308.                                     begin
  1309.                                         h^^.whNext := h2^^.whNext;
  1310.                                         h := nil;
  1311.                                     end;
  1312.                                 if h <> nil then
  1313.                                     h := h2;
  1314.                             end;
  1315.                     end;
  1316.                 if not returnflag then
  1317.                     begin
  1318.                         if (whClobOnRmve) then
  1319.                             DoClobber(h2);
  1320.                         DisposHandle(Handle(h2));
  1321.                     end;
  1322.             end;
  1323.     end;
  1324.  
  1325. {$IFC supportDialogs }
  1326.  
  1327. { -------------------------------------------------------------------- }
  1328. {                    Dialog-handler interface routines                                }
  1329. { -------------------------------------------------------------------- }
  1330.  
  1331.  
  1332.  
  1333. {    Install a dialog handler.  Remove any previous handler for it.}
  1334. {    SkelDialog calls SkelWindow as a subsidiary to install a window}
  1335. {    handler, then sets the event procedure on return.}
  1336.  
  1337. {    Pass the following parameters:}
  1338.  
  1339. {    theDialog    Pointer to the dialog to be handled.  Must be created}
  1340. {            by host.}
  1341. {    pEvent    Event-handling proc for dialog events.}
  1342. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1343. {            mainly to dialog handlers that want to know when to}
  1344. {            self-destruct (with SkelRmveDlog).}
  1345. {    pClobber Proc for disposal of handler's data structures}
  1346.  
  1347. {    If a particular procedure is not needed, pass nil in place of}
  1348. {    the appropriate procedure address.}
  1349.  
  1350. {    Return false if no handler could be allocated, true if successful.}
  1351.  
  1352.     function SkelDialog;
  1353.  
  1354.         var
  1355.             wh: WHandlerHnd;
  1356.             aBool: Boolean;
  1357.  
  1358.     begin
  1359.         aBool := SkelWindow(theDialog, nil, nil, nil, nil, pClose, pClobber, nil, false);
  1360.         if aBool <> false then
  1361.             begin
  1362.                 wh := GetWDHandler(theDialog);
  1363.                 wh^^.whEvent := pEvent;
  1364. {Added by Ingemar 18/9 -93:}
  1365.                 wh^^.whFilter := pFilter; {Install a filter function to be called *before* IsDialogEvent!}
  1366.             end;
  1367.         SkelDialog := aBool;
  1368.     end;
  1369.  
  1370. {    Remove a dialog and its handler}
  1371.  
  1372.     procedure SkelRmveDlog;
  1373.  
  1374.     begin
  1375.         SkelRmveWind(theDialog);
  1376.     end;
  1377. {$ENDC}
  1378. { -------------------------------------------------------------------- }
  1379. {                    Miscellaneous interface routines                    }
  1380. { -------------------------------------------------------------------- }
  1381.  
  1382.  
  1383. {    Override the default sizing limits for a window, or, if theWind}
  1384. {    is nil, reset the default limits used by SkelWindow.}
  1385.  
  1386.     procedure SkelGrowBounds;
  1387.  
  1388.         var
  1389.             h: WHandlerHnd;
  1390.             r: Rect;
  1391.  
  1392.     begin
  1393.         if theWind = nil then
  1394.             SetRect(growRect, hLo, vLo, hHi, vHi)
  1395.         else
  1396.             begin
  1397.                 h := GetWHandler(theWind);
  1398.                 if h <> nil then
  1399.                     begin
  1400.                         SetRect(r, hLo, vLo, hHi, vHi);
  1401.                         h^^.whGrow := r;
  1402.                     end;
  1403.             end;
  1404.     end;
  1405.  
  1406. {    Set the event mask.}
  1407.  
  1408.     procedure SkelEventMask;
  1409.  
  1410.     begin
  1411.         eventMask := mask;
  1412.     end;
  1413.  
  1414. {    Return the event mask.}
  1415.  
  1416.     procedure SkelGetEventMask;
  1417.  
  1418.     begin
  1419.         mask := eventMask;
  1420.     end;
  1421.  
  1422. {    Install a background task.  If p is nil, the current task is}
  1423. {    disabled.}
  1424.  
  1425.     procedure SkelBackground;
  1426.  
  1427.     begin
  1428.         pBkgnd := p;
  1429.     end;
  1430.  
  1431. {    Return the current background task.  Return nil if none.}
  1432.  
  1433.     procedure SkelGetBackground;
  1434.     begin
  1435.         p := pBkgnd;
  1436.     end;
  1437.  
  1438. {    Install an event-inspecting hook.  If p is nil, the hook is}
  1439. {    disabled.}
  1440.  
  1441.     procedure SkelEventHook;
  1442.  
  1443.     begin
  1444.         pEvent := p;
  1445.     end;
  1446.  
  1447.     procedure SkelGetEventHook;
  1448.  
  1449.     begin
  1450.         p := pEvent;
  1451.     end;
  1452. {$IFC supportDialogs }
  1453.  
  1454. {    Set the mask for event types that will be passed to dialogs.}
  1455. {    Bit 1 is always set, so that null events will be passed.}
  1456. {    If this is not done, the caret does not blink in editText items.}
  1457.  
  1458.     procedure SkelDlogMask;
  1459.  
  1460.     begin
  1461.         dlogEventMask := BitOr(mask, 1);
  1462.     end;
  1463.  
  1464. {    Return the current dialog event mask.}
  1465.  
  1466.     procedure SkelGetDlogMask;
  1467.  
  1468.     begin
  1469.         mask := dlogEventMask;
  1470.     end;
  1471. {$ENDC}
  1472. end.